<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\CommonBase;
use think\facade\View;
use think\response\Json;

class ModuleField extends AdminBase
{
    public function lists(): string
    {
        $moduleId = $this->request->param("module_id", 0, 'intval');
        if (empty($moduleId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $moduleBusiness = new \app\common\business\Module($this->websiteCode);
        $module         = $moduleBusiness->findModuleById($moduleId);
        if (empty($module)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign([
            'moduleId'      => $moduleId,
            'moduleName'    => $module['name'],
            'moduleListUrl' => url('module/lists', ['website_code' => $this->websiteCode])]);
        return View::fetch();
    }

    public function list(): Json
    {
        $moduleId            = $this->request->param("module_id", 0, 'intval');
        $page                = $this->request->param('page', 1, 'intval');
        $limit               = $this->request->param('limit', 10, 'intval');
        $name                = $this->request->param('name', '', 'trim');
        $status              = $this->request->param('status', 0, 'intval');
        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);

        $where = ['module_id' => $moduleId];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        $moduleInfo = $moduleFieldBusiness->getModuleFieldList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $moduleInfo['rows'], $moduleInfo['count']);
    }

    public function add(): string
    {
        $moduleId = $this->request->param("module_id", 0, 'intval');
        View::assign(["moduleId" => $moduleId]);
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $sort      = $this->request->param("sort", 0, 'intval');
        $moduleId  = $this->request->param("module_id", 0, 'intval');
        $name      = $this->request->param('name', '', 'trim');
        $code      = $this->request->param('code', '', 'trim');
        $fieldType = $this->request->param('field_type', '', 'trim');
        $isRequire = $this->request->param('is_require', '', 'intval');
        $showList  = $this->request->param('show_list', '', 'intval');
        $status    = $this->request->param('status', 0, 'intval');

        if (!$moduleId) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $data     = [
            'sort'         => $sort,
            'module_id'    => $moduleId,
            'name'         => $name,
            'code'         => $code,
            'field_type'   => $fieldType,
            'is_require'   => $isRequire == CommonBase::FIELD_IS_REQUIRE ? CommonBase::FIELD_IS_REQUIRE : CommonBase::FIELD_IS_NOT_REQUIRE,
            'is_system'    => CommonBase::FIELD_IS_CUSTOMER,
            'show_list'    => $showList == CommonBase::FIELD_IS_SHOW_LIST ? CommonBase::FIELD_IS_SHOW_LIST : CommonBase::FIELD_IS_HIDDEN_LIST,
            'status'       => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'website_code' => $this->websiteCode,
        ];
        $validate = (new \app\admin\validate\ModuleField())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);
        $res                 = $moduleFieldBusiness->addModuleField($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);
        $moduleField         = $moduleFieldBusiness->findModuleFieldById($id);
        if (empty($moduleField)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["field" => $moduleField]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id        = $this->request->param('id', 0, 'intval');
        $sort      = $this->request->param("sort", 0, 'intval');
        $name      = $this->request->param('name', '', 'trim');
        $isRequire = $this->request->param('is_require', '', 'intval');
        $showList  = $this->request->param('show_list', '', 'intval');
        $status    = $this->request->param('status', 0, 'intval');

        if (!$id) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);
        $moduleField         = $moduleFieldBusiness->findModuleFieldById($id);
        if (empty($moduleField)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $data = ['name' => $name, 'sort' => $sort, 'status' => self::_IS_ENABLE_STATUS];
        if ($moduleField['is_system'] != CommonBase::FIELD_IS_SYSTEM) {
            $data['is_require'] = $isRequire == CommonBase::FIELD_IS_REQUIRE ? CommonBase::FIELD_IS_REQUIRE : CommonBase::FIELD_IS_NOT_REQUIRE;
            $data['show_list']  = $showList == CommonBase::FIELD_IS_SHOW_LIST ? CommonBase::FIELD_IS_SHOW_LIST : CommonBase::FIELD_IS_HIDDEN_LIST;
            $data['status']     = $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS;
        }

        $validate = (new \app\admin\validate\ModuleField())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);
        $res                 = $moduleFieldBusiness->updateModuleField($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);
        $res                 = $moduleFieldBusiness->deleteModuleField($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $data = [];
        foreach ($idArr as $k => $v) {
            $data[$v] = $sortArr[$k];
        }

        $moduleFieldBusiness = new \app\common\business\ModuleField($this->websiteCode);
        $res                 = $moduleFieldBusiness->batchModuleFieldSort($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}
